package com.baomidou.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.entity.OracleKeyGenerator;
import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.springboot.config.properties.DruidProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.baomidou.springboot.mapper*")
public class MybatisPlusConfig {

    @Autowired
    DruidProperties druidProperties;

    private static final Logger logger = LoggerFactory.getLogger(MybatisPlusConfig.class);
	/**
	 * mybatis-plus分页插件<br>
	 * 文档：http://mp.baomidou.com<br>
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		paginationInterceptor.setDialectType("oracle");
		//paginationInterceptor.setDialectType(DBType.H2.getDb());
		//paginationInterceptor.setOptimizeType(Optimize.JSQLPARSER.getOptimize());
		return paginationInterceptor;
	}

	@Bean(initMethod = "init")
	public DruidDataSource dataSource() {
	    DruidDataSource dataSource = new DruidDataSource();
	    druidProperties.coinfig(dataSource);
	    return dataSource;
    }


    @Bean
	public GlobalConfiguration globalConfiguration() {
	    logger.info("---------------------- Global Config ---------------------------");
		GlobalConfiguration conf = new GlobalConfiguration();
		conf.setDbColumnUnderline(true);
		conf.setDbType("oracle");
		conf.setIdType(1);
		conf.setKeyGenerator(new OracleKeyGenerator());
		return conf;
	}
}
